package com.bsj.power.common.config.global;

import lombok.Data;

import java.io.Serializable;

/**
 * @author yinhao
 * @version 1.0
 * @description 全局返回对象
 * @date 2022/8/26
 */
@Data
public class JsonResult<T> implements Serializable {

    /**
     * 返回码 200为成功
     */
    private int code;
    /**
     * 消息提示
     */
    private String msg;
    /**
     * 返回的数据
     */
    private T data;
    /**
     * 总条数
     */
    private long total;

    /**
     * 生成统一返回对象
     *
     * @param data
     * @param code
     * @param msg
     * @param total
     * @param <T>
     * @return
     */
    private static <T> JsonResult<T> toResult(T data, int code, String msg, long total) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        result.setTotal(total);
        return result;
    }

    private static <T> JsonResult<T> toResult(T data, int code, String msg) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> JsonResult<T> success() {
        return toResult(null, JsonResultEnum.SUCCESS.getCode(), JsonResultEnum.SUCCESS.getMsg(), 0);
    }

    public static <T> JsonResult<T> success(T data) {
        return toResult(data, JsonResultEnum.SUCCESS.getCode(), JsonResultEnum.SUCCESS.getMsg(), 0);
    }

    public static <T> JsonResult<T> success(JsonResultEnum response) {
        return toResult(null, response.getCode(), response.getMsg(), 0);
    }

    public static <T> JsonResult<T> success(JsonResultEnum response, T data) {
        return toResult(data, response.getCode(), response.getMsg(), 0);
    }

    public static <T> JsonResult<T> success(T data, long total) {
        return toResult(data, JsonResultEnum.SUCCESS.getCode(), JsonResultEnum.SUCCESS.getMsg(), total);
    }

    public static <T> JsonResult<T> success(JsonResultEnum response, T data, long total) {
        return toResult(data, response.getCode(), response.getMsg(), total);
    }

    public static <T> JsonResult<T> success(int code, String msg) {
        return toResult(null, code, msg, 0);
    }

    public static <T> JsonResult<T> success(String msg, T data) {
        return toResult(data, JsonResultEnum.SUCCESS.getCode(), msg, 0);
    }

    /**
     * 失败
     */
    public static <T> JsonResult<T> fail() {
        return toResult(null, JsonResultEnum.FAIL.getCode(), JsonResultEnum.FAIL.getMsg(), 0);
    }

    public static <T> JsonResult<T> fail(JsonResultEnum response) {
        return toResult(null, response.getCode(), response.getMsg(), 0);
    }

    public static <T> JsonResult<T> fail(JsonResultEnum response, String msg) {
        return toResult(null, response.getCode(), msg, 0);
    }

    public static <T> JsonResult<T> fail(JsonResultEnum response, T data) {
        return toResult(data, response.getCode(), response.getMsg(), 0);
    }

    public static <T> JsonResult<T> fail(String msg) {
        return toResult(null, JsonResultEnum.FAIL.getCode(), msg, 0);
    }

    public static <T> JsonResult<T> paramError() {
        return toResult(null, JsonResultEnum.PARAM_ERROR.getCode(), JsonResultEnum.PARAM_ERROR.getMsg());
    }
}
